﻿using log4net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Infrastructure
{
    /// <summary>
    /// Log4Net
    /// </summary>
    public static class Log4NetSetup
    {
        /// <summary>
        /// Log4 Microsoft.Extensions.Logging.Log4Net.AspNetCore
        /// </summary>
        /// <param name="services"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void AddLog4NetSetup(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            //ILog log = LogManager.GetLogger("NETCoreRepository", this.GetType());   //Assembly.GetEntryAssembly()
            ILog log = LogManager.GetLogger("NETCoreRepository", System.Reflection.Assembly.GetExecutingAssembly().GetType());   //Assembly.GetEntryAssembly()
            services.AddLogging(cfg =>
            {
                cfg.AddLog4Net();
                //默认的配置文件路径是在根目录，且文件名为log4net.config
                //如果文件路径或名称有变化，需要重新设置其路径或名称
                //比如在项目根目录下创建一个名为cfg的文件夹，将log4net.config文件移入其中，并改名为log.config
                //则需要使用下面的代码来进行配置
                //cfg.AddLog4Net(new Log4NetProviderOptions()
                //{
                //    Log4NetConfigFileName = "cfg/Log4net.config",
                //    Watch = true
                //});
            });
        }

        #region net5.0

        //public static class Log4NetExtensions
        //{
        //    public static IHostBuilder UseLog4Net(this IHostBuilder hostBuilder)
        //    {
        //        var log4netRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
        //        XmlConfigurator.Configure(log4netRepository, new FileInfo("log4net.config"));

        //        return hostBuilder;
        //    }
        //}

        #endregion
    }
}
